package com.csq.search.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.csq.common.pojo.SearchResultData;
import com.csq.manager.pojo.Parttime;
import com.csq.search.dao.SearchDao;

@Repository
public class SearchDaoImpl implements SearchDao{

	
	@Autowired
	private SolrServer solrServer;
	
	@Override
	public SearchResultData<Parttime> searchParttime(SolrQuery solrQuery) throws SolrServerException {
		// TODO Auto-generated method stub
		
		QueryResponse query = solrServer.query(solrQuery);
		
		
		Map<String, Map<String, List<String>>> highlighting = query.getHighlighting();
		
		SolrDocumentList results = query.getResults();
		ArrayList<Parttime> arrayList = new ArrayList<Parttime>();
		for (SolrDocument solrDocument : results) {
			Parttime parttime = new Parttime();
			parttime.setParttimeId((Integer)solrDocument.get("id"));
			
			Map<String, List<String>> highResult = highlighting.get(solrDocument.get("id"));
			
			String parttimeName = "";
			if(highResult != null && highResult.size() > 0){
				parttimeName = highResult.get("parttime_name").get(0);
			}else{
				parttimeName= (String) solrDocument.get("parttime_name");
			}
			
			parttime.setParttimeName(parttimeName);
			
			arrayList.add(parttime);
		}
		
		SearchResultData<Parttime> searchResultData = new SearchResultData<Parttime>();
		searchResultData.setResultList(arrayList);
		searchResultData.setRecordCount(results.getNumFound());
		return searchResultData;
	}

}
